
import java.awt.*;
import javax.swing.*;

/**
 * Clase para manejar los componentes de cada una de las etapas.
 *
 * @author Villalva Bueno Arely Eunice
 * @author Romero Rodriguez Gabriel Eduardo
 */
public class Stages extends Panel {

    JTextArea fetch = new JTextArea(5, 10);
    JTextArea decode = new JTextArea(5, 10);
    JTextArea execute = new JTextArea(5, 10);
    JTextArea wb = new JTextArea(5, 10);
    JTextArea memory = new JTextArea(5, 10);
    JTextField cpi = new JTextField();
    JTextField nInstr = new JTextField();
    JTextField nCycles = new JTextField();
    Canvas filler = new Canvas();

    /**
     * Fija los valores de los componentes
     */
    void buildConstraints(GridBagConstraints gbc, int gx, int gy,
            int gw, int gh, int wx, int wy) {
        gbc.gridx = gx;
        gbc.gridy = gy;
        gbc.gridwidth = gw;
        gbc.gridheight = gh;
        gbc.weightx = wx;
        gbc.weighty = wy;
    }

    Stages() {
        GridBagLayout gridbag = new GridBagLayout();
        GridBagConstraints constraints = new GridBagConstraints();
        setLayout(gridbag);


        buildConstraints(constraints, 0, 0, 5, 1, 100, 15);
        constraints.fill = GridBagConstraints.BOTH;
        constraints.anchor = GridBagConstraints.CENTER;
        gridbag.setConstraints(filler, constraints);
        add(filler);


        buildConstraints(constraints, 0, 1, 1, 1, 20, 15);
        constraints.fill = GridBagConstraints.BOTH;
        constraints.anchor = GridBagConstraints.CENTER;
        Label Lfetch = new Label("Fetch", Label.CENTER);
        gridbag.setConstraints(Lfetch, constraints);
        add(Lfetch);

        buildConstraints(constraints, 1, 1, 1, 1, 20, 15);
        constraints.fill = GridBagConstraints.BOTH;
        constraints.anchor = GridBagConstraints.CENTER;
        Label Ldecode = new Label("Decodificacion", Label.CENTER);
        gridbag.setConstraints(Ldecode, constraints);
        add(Ldecode);

        //Etiqueta de Ejecución
        buildConstraints(constraints, 2, 1, 1, 1, 20, 15);
        constraints.fill = GridBagConstraints.BOTH;
        constraints.anchor = GridBagConstraints.CENTER;
        Label Lex = new Label("Ejecución", Label.CENTER);
        gridbag.setConstraints(Lex, constraints);
        add(Lex);

        // Etiqueta de memoria
        buildConstraints(constraints, 3, 1, 1, 1, 20, 15);
        constraints.fill = GridBagConstraints.BOTH;
        constraints.anchor = GridBagConstraints.CENTER;
        Label Lmem = new Label("Memoria", Label.CENTER);
        gridbag.setConstraints(Lmem, constraints);
        add(Lmem);

        
        buildConstraints(constraints, 4, 1, 1, 1, 20, 15);
        constraints.fill = GridBagConstraints.BOTH;
        constraints.anchor = GridBagConstraints.CENTER;
        Label Lwb = new Label("Write Back", Label.CENTER);
        gridbag.setConstraints(Lwb, constraints);
        add(Lwb);

        // Fetch textarea
        buildConstraints(constraints, 0, 2, 1, 1, 20, 60);
        constraints.fill = GridBagConstraints.BOTH;
        constraints.anchor = GridBagConstraints.CENTER;
        gridbag.setConstraints(fetch, constraints);
        fetch.setEditable(false);
        add(fetch);

        // Decodificacion textArea
        buildConstraints(constraints, 1, 2, 1, 1, 20, 60);
        constraints.fill = GridBagConstraints.BOTH;
        constraints.anchor = GridBagConstraints.CENTER;
        gridbag.setConstraints(decode, constraints);
        decode.setEditable(false);
        add(decode);

        // Ejecucion textArea
        buildConstraints(constraints, 2, 2, 1, 1, 20, 60);
        constraints.fill = GridBagConstraints.BOTH;
        constraints.anchor = GridBagConstraints.CENTER;
        gridbag.setConstraints(execute, constraints);
        execute.setEditable(false);
        add(execute);

        // Memoria textArea
        buildConstraints(constraints, 3, 2, 1, 1, 20, 60);
        constraints.fill = GridBagConstraints.BOTH;
        constraints.anchor = GridBagConstraints.CENTER;
        gridbag.setConstraints(memory, constraints);
        memory.setEditable(false);
        add(memory);

        // WB textArea
        buildConstraints(constraints, 4, 2, 1, 1, 20, 60);
        constraints.fill = GridBagConstraints.BOTH;
        constraints.anchor = GridBagConstraints.CENTER;
        gridbag.setConstraints(wb, constraints);
        wb.setEditable(false);
        add(wb);

        Panel stats = new Panel();
        stats.setLayout(new GridLayout(1, 6, 10, 10));

        Label Lni = new Label("Instrucciones:", Label.RIGHT);
        Label Lnc = new Label("Ciclos:", Label.RIGHT);
        Label Lcpi = new Label("CPI:", Label.RIGHT);
        nInstr.setEditable(false);
        nCycles.setEditable(false);
        cpi.setEditable(false);

        stats.add(Lni);
        stats.add(nInstr);
        stats.add(Lnc);
        stats.add(nCycles);
        stats.add(Lcpi);
        stats.add(cpi);

        buildConstraints(constraints, 0, 3, 5, 1, 100, 10);
        constraints.fill = GridBagConstraints.BOTH;
        constraints.anchor = GridBagConstraints.CENTER;
        gridbag.setConstraints(stats, constraints);
        add(stats);

        validate();
    }

    /**
     * Limpia todos los campos de texto
     */
    public void clearAll() {
        fetch.setText("");
        decode.setText("");
        execute.setText("");
        memory.setText("");
        wb.setText("");
    }
}
